<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商户"
        >
          <a-select :disabled="isDisabled"
                    showSearch
                    optionFilterProp="children"
                    :filterOption="filterOption"
                    v-decorator="[ 'merchantId', {rules: [{ required: true, message: '请选择。。。' }]} ]">
            <a-select-option v-for="item in businessList" :key="item.id">{{item.name}}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="会员卡名称"
        >
          <a-input  v-decorator="[ 'cardName', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="会员折扣"
        >
          <a-input  placeholder="必须是0到1之间" v-decorator="[ 'discount', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="单次充值多少达标"
        >
          <a-input  placeholder='单次充值多少达标' v-decorator="[ 'onceFee', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="是否注册默认等级">
          <a-radio-group v-decorator="[ 'isDefault', {rules: [{ required: true, message: '请选择' }]}]">
            <a-radio :value="1">是</a-radio>
            <a-radio :value="0">否</a-radio>
          </a-radio-group>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {addOrUpdate} from '@/api/merchantService/memberLevel'
  import { selectAll} from '@/api/merchantService/merchant'
  import pick from 'lodash.pick'
  export default {
    data(){
      return{
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        isDisabled: false,
        businessList: [],
      }
    },
    created() {
      selectAll().then(res => {

        this.businessList = res.result
      })

    },
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      filterOption(input, option) {
        return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      },
      edit(record) {
        this.isDisabled = true
        record.onceFee = record.onceFee/100;
        this.mdl = Object.assign({}, record)
        this.visible = true
        if(record.status){
          this.mdl.status = 1
        }else{
          this.mdl.status = 0
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'merchantId','cardName', 'discount','onceFee','isDefault'))
        })


      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl={}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            _this.confirmLoading = true
            // 模拟后端请求 2000 毫秒延迟
            values.type=1
            values.onceFee = values.onceFee*100
            addOrUpdate(values).then((res) => {
              console.log('添加',res)
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              }else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>